package com.wutuo.aggragation.rollcall.controller;

import cn.apestar.agg.common.security.BaseAuthController;
import com.wutuo.aggragation.rollcall.dao.TaskDetailMapper;
import com.wutuo.aggragation.rollcall.request.*;
import com.wutuo.aggragation.rollcall.response.AddRollCallTaskResponse;
import com.wutuo.aggragation.rollcall.response.ListSignDetailResponse;
import com.wutuo.aggragation.rollcall.response.ListSignResponse;
import com.wutuo.aggragation.rollcall.service.RollCallTaskService;
import com.wutuo.aggragation.rollcall.vo.RollCallListVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/call", produces = "application/json;charset=utf-8")
public class RollCallController extends BaseAuthController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RollCallTaskService rollCallTaskService;

    @Autowired
    private TaskDetailMapper taskDetailMapper;

    /**
     * 点名任务列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public List<RollCallListVo> list(@RequestBody ListRollCallTaskRequest request) {

        return rollCallTaskService.list(request, getInstitutionId());
    }

    /**
     * 新增点名任务
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public AddRollCallTaskResponse add(@RequestBody AddRollCallTaskRequest request) {

        request.setInsitutionId(getInstitutionId());
        request.setTeacherId(getCurrentUserId());
        return rollCallTaskService.add(request);
    }

    @RequestMapping(value = "/signlist", method = RequestMethod.POST)
    public ListSignResponse add(@RequestBody ListSignRequest request) {

        return rollCallTaskService.listSign(request, getInstitutionId());
    }

    @RequestMapping(value = "/child", method = RequestMethod.POST)
    public void sign(@RequestBody @Valid AddSignRequest request) {

        request.setTeacherId(getCurrentUserId());
        rollCallTaskService.sign(request, getCurrentUser());
    }

    /**
     * 列出签到记录.
     *
     * @param callId   午托任务Id
     * @param typeId   午托类型
     * @param sortType 排序类型：sign:签到时间(默认)、class:班级年级、 teacher:老师、leave：离开时间
     * @param state    签到和未签：all ，已签：sign，未签：miss
     * @return 根据状态显示搜索到的签到记录
     */
    // @RequestMapping(value = "/sign/list", method = RequestMethod.GET)
    // public ListSignResponse list(@RequestParam("call_id") Long callId,
    // @RequestParam(value = "type_id", required = false) Long typeId,
    // @RequestParam(value = "sort_type", defaultValue = "sign", required =
    // false) String sortType,
    // @RequestParam(value = "state", defaultValue = "all", required = false)
    // String state) {
    //
    // ListSignRequest request = new ListSignRequest();
    // request.setTaskId(callId);
    // request.setTypeId(typeId);
    // // request.setYear(year);
    // // request.setTagId(tagId);
    //
    // List<ListSignItem> signRecords = Collections.emptyList();
    // List<ListSignItem> unSignRecords = Collections.emptyList();
    // switch (state) {
    // case "unsign":
    // unSignRecords = rollCallTaskService.listSign(callId, getInstitutionId(),
    // request);
    // break;
    // case "sign":
    // signRecords = rollCallTaskService.listUnsign(callId, getInstitutionId(),
    // request);
    // break;
    // default:
    // signRecords = rollCallTaskService.listSign(callId, getInstitutionId(),
    // request);
    // unSignRecords = rollCallTaskService.listUnsign(callId,
    // getInstitutionId(), request);
    // break;
    // }
    //
    // logger.info("sort_type:{},state:{}", sortType, state);
    //
    // // Get task by id
    // Task task = rollCallTaskService.getTaskById(callId);
    // if (task == null) {
    // throw new BusinessException("task.not.exist");
    // }
    // CallVo callTaskVo = RollCallDataConvert.convertTaskToCallVo(task);
    //
    // int lateCount =
    // taskDetailMapper.countSignedChildren(RollCallState.LATE.getValue(),
    // callId, null);
    //
    // ListSignResponse response = new ListSignResponse();
    // response.setCall(callTaskVo);
    // response.setLateNum(lateCount < 0 ? 0 : lateCount);
    // response.setSign(signRecords);
    // response.setSignNum(signRecords == null ? 0 : signRecords.size());
    // response.setUnsign(unSignRecords);
    // response.setLateNum(unSignRecords.size() + lateCount);
    //
    // return response;
    //
    // }
    @RequestMapping(value = "/sign/list", method = RequestMethod.GET)
    public ListSignDetailResponse listSignDetail(@RequestParam("call_id") Long callId,
                                                 @RequestParam(value = "type_id", required = false) Long typeId,
                                                 @RequestParam(value = "sort_type", defaultValue = "sign", required = false) String sortType) {

        ListSignRequest request = new ListSignRequest();
        request.setTaskId(callId);
        request.setTypeId(typeId);
        // request.setYear(year);
        // request.setTagId(tagId);

        return rollCallTaskService.listSignDetail(request, getInstitutionId());
    }

    @RequestMapping(value = "/sign/list/del", method = RequestMethod.POST)
    public void delSignRecord(@RequestBody DelSignRecordRequest request) {
        rollCallTaskService.delSignRecord(request.getCallId(), request.getChidId(), getCurrentUserId());
    }

}
